package com.fy56.platform.logistics.restful.api.controller.wx;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.fy56.platform.logistics.restful.api.utils.AlipayUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import com.wordnik.swagger.annotations.Api;
import com.zeus.base.privilege.utils.ResultModel;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 支付宝小程序支付Controller控制器
 *
 * @author zhaoyf
 * @version 2021-03-17
 */
@Api(value = "AliPay", description = "支付宝小程序支付控制器", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/AliPay")
public class AliPayController {
    private static Logger log = Logger.getLogger(AliPayController.class);

    @Autowired
    private AlipayUtil alipayUtil;


    @RequestMapping(value = "/alipayUserInfo", method = RequestMethod.GET)
    public ResultModel alipayUserInfo(@RequestParam String authCode, HttpServletRequest request ) throws Exception {
        ResultModel result = new ResultModel(true, "操作保存成功");
        if(StringUtil.isEmpty(authCode)){
            return new ResultModel(false, "授权编码不能为空!");
        }
        try{
            AlipayClient alipayClient = new DefaultAlipayClient(alipayUtil.getGateWay(),alipayUtil.getAppId(),
                    alipayUtil.getPrivateKey(), alipayUtil.getAliFormat(), alipayUtil.getAliCharset(), alipayUtil.getPublicKey(), alipayUtil.getSignType());
            AlipaySystemOauthTokenRequest requests = new AlipaySystemOauthTokenRequest();
            requests.setCode(authCode);
            requests.setGrantType("authorization_code");
            AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(requests);
            result.setObject(oauthTokenResponse);
        }catch(Exception e){
            e.printStackTrace();
            return new ResultModel(false, "系统异常，请联系管理员!");
        }
        return result;
    }
}